﻿using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Filesharing
{
    public partial class Upload : Form
    {
        private int projectID;
        private string ftp = "ftp://ftp.qq-design.com";
        private string usn = "alex";
        private string pass = "Huibenalexmoetengoedophunwachtwoordletten";
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn); 
        private string pathname;
        private string ftpfilepath;

        public Upload()
        {
            
        }

        public Upload(int projectid)
        {
            projectID = projectid;
            InitializeComponent();
        }

        private void UploadFile(string FTPAddress, string filePath)
        {
            try
            {
                //Create FTP request
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress + "/" + projectID + "/" + Path.GetFileName(filePath)));

                //pathnaam ophalen om in db te stoppen
                pathname = Path.GetFileName(filePath);
                ftpfilepath = ftp + '/' + projectID + '/' + pathname;
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(usn, pass);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;

                //bestand laden
                FileStream stream = File.OpenRead(filePath);
                //bestand naar bytearray schrijven
                byte[] buffer = new byte[stream.Length];

                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                //bestand uploaden
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Close();

                MessageBox.Show("Het bestand is geüpload");
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Kies een bestand om te uploaden");
            }
            
            //filepath uploaden naar database om zo downloads te regelen
            try
            {
                string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                MySqlCommand filepathUpload = new MySqlCommand("INSERT INTO doc_upload (DocID, Docname, DocPath, ProjectID, Entrydate)" + "VALUES ('','"+pathname+"', '"+ftpfilepath+"', '"+projectID+"', '"+datetime+"')", grabConnection);
                grabConnection.Open();
                filepathUpload.ExecuteNonQuery();
            }
            catch(MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Dispose();
            }        
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            Application.DoEvents();

            UploadFile(ftp, txtFilePath.Text);
            btnUpload.Enabled = true;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = openFile.FileName;
            }
        }
    }
}
